//
//  PublishView.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/24.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//


//点击了tabbar中间的按钮之后 跳出来的选择界面

import UIKit

class PublishView: UIViewController {
    
    var blurEffect:UIBlurEffect!//模糊效果
    var blurView:UIVisualEffectView!//模糊试图
    var centerIcon:UIButton!//中间按钮
    var shareButton:UIButton!//我有干货 我要分享按钮
    var questionButton:UIButton!//提问按钮
    var shareLabel:UILabel!//分享的文字
    var questionLabel:UILabel!//提问的文字
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.createBlurView()
        self.createChoiceButton()
        self.createCenterIcon()
        self.createLabel()
        self.animateForCenterIcon()
        self.animateForButtonPush()
    }
}

//MARK:--UI设置
extension PublishView{
    //创建视图的模糊效果
    func createBlurView(){
        self.view.backgroundColor = UIColor.clearColor()
        self.blurEffect = UIBlurEffect(style: .Dark)
        self.blurView = UIVisualEffectView(effect: blurEffect)
        self.blurView.frame = MainBounds
        self.blurView.alpha = 0.97
        self.view.addSubview(blurView)
        blurView.userInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.blurViewTouchClick(_:)))
        blurView.addGestureRecognizer(tap)
    }
    
    //创建弹出的两个选择按钮
    func createChoiceButton(){
        shareButton = UIButton(frame: CGRect(x: 154*SCALEW,y: HeightAPP-67.3,width: 64,height: 64))
        shareButton.setBackgroundImage(UIImage(named: "shareIcon"), forState: UIControlState())
        shareButton.addTarget(self, action: #selector(self.shareButtonClick(_:)), forControlEvents: .TouchUpInside)
        blurView.addSubview(shareButton)
        
        questionButton = UIButton(frame: CGRect(x: 154*SCALEW,y: HeightAPP-67.3,width: 64,height: 64))
        questionButton.setBackgroundImage(UIImage(named: "questionIcon"), forState: UIControlState())
        questionButton.addTarget(self, action: #selector(questionButtonClick(_:)), forControlEvents: .TouchUpInside)
        blurView.addSubview(questionButton)
    }
    
    //创建浮在模糊视图上面的centerIcon
    func createCenterIcon(){
        centerIcon = UIButton(frame: CGRect(x: 154*SCALEW,y: HeightAPP-67.3,width: 64,height: 64))
        centerIcon.setImage(UIImage(named: "moreIcon"), forState: UIControlState())
        centerIcon.addTarget(self, action: #selector(self.centerIconClick(_:)), forControlEvents: .TouchUpInside)
        blurView.addSubview(centerIcon)
    }
    
    //创建两个文本提示
    func  createLabel(){
        shareLabel = UILabel(frame: CGRect(x: 33*SCALEW,y: 554*SCALEH,width: 141.5*SCALEW,height: 18))
        shareLabel.textColor = UIColor.whiteColor()
        shareLabel.text = "我有干货 我要分享"
        shareLabel.textAlignment = .Center
        shareLabel.alpha = 0
        shareLabel.font = BaseFunc.getSystemFont(15)
        self.view.addSubview(shareLabel)
        
        questionLabel = UILabel(frame: CGRect(x: 201*SCALEW,y: 554*SCALEH,width: 141.5*SCALEW,height: 18))
        questionLabel.textColor = UIColor.whiteColor()
        questionLabel.text = "我有问题 我要提问"
        questionLabel.textAlignment = .Center
        questionLabel.alpha = 0
        questionLabel.font = BaseFunc.getSystemFont(15)
        self.view.addSubview(questionLabel)
    }
    
    
    
    
}


//MARK:--点击事件 + 动画等
extension PublishView{
    //干货分享点击事件
    func shareButtonClick(sender:UIButton){
        self.animateToRemoveView()
        self.presentViewController(EditViewController(), animated: true, completion: nil)
    }
    
    //提问点击事件
    func questionButtonClick(sender:UIButton){
        self.animateToRemoveView()
        self.presentViewController(EditViewController(), animated: true, completion: nil)
    }
    
    //模糊视图点击事件
    func blurViewTouchClick(sender:UITapGestureRecognizer){
        self.animateToRemoveView()
    }
    
    //centerIcon点击事件
    func centerIconClick(sender:UIButton){
        self.animateToRemoveView()
    }
    
    //收回视图的动画
    func animateToRemoveView(){
        //收回一个button
        UIView.animateWithDuration( 0.1, delay: 0, options: .CurveEaseOut, animations: {
            self.shareButton.center.x = 105*SCALEW
            self.shareButton.center.y = 493.5*SCALEH
            self.shareLabel.alpha = 0
        }) { (end) in
            UIView.animateWithDuration(0.1, delay: 0.05, options: .CurveEaseOut, animations: {
                self.shareButton.center.x = WidthAPP/2
                self.shareButton.center.y = HeightAPP-33.65
                self.shareButton.transform = CGAffineTransformMakeScale(0.5, 0.5)
                
            }) { (end) in
                
            }
        }
        
        //收回一个button
        UIView.animateWithDuration(0.1, delay: 0.15, options: .CurveEaseOut, animations: {
            self.questionButton.center.x = 273.5*SCALEW
            self.questionButton.center.y = 493.5*SCALEH
            self.questionLabel.alpha = 0
        }) { (end) in
            UIView.animateWithDuration( 0.1, delay: 0.05, options: .CurveEaseOut, animations: {
                self.questionButton.center.x = WidthAPP/2
                self.questionButton.center.y = HeightAPP-33.65
                self.questionButton.transform = CGAffineTransformMakeScale(0.5, 0.5)
                
            }) { (end) in
                UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: {
                    self.view.alpha = 0
                }) { (end) in
                    self.view.removeFromSuperview()
                }
            }
        }
        
        //将centerIcon转回来
        UIView.animateWithDuration(0.6, delay: 0.1, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.7, options: .CurveEaseOut, animations: {
            self.centerIcon.transform = CGAffineTransformRotate(self.centerIcon.transform, -CGFloat(M_PI_4))
            }, completion: nil)
    }
    
    //centerIcon的动画
    func animateForCenterIcon(){
        UIView.animateWithDuration(0.6, delay: 0.1, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.7, options: .CurveEaseOut, animations: {
            self.centerIcon.transform = CGAffineTransformRotate(self.centerIcon.transform, CGFloat(M_PI_4))
            }, completion: nil)
    }
    
    
    //按钮弹出动画
    func animateForButtonPush(){
        UIView.animateWithDuration(0.6, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: .CurveEaseOut, animations: {
            self.shareButton.center.x = 103*SCALEW
            self.shareButton.center.y = 495.5*SCALEH
            self.shareButton.transform = CGAffineTransformMakeScale(1.3, 1.3)
            self.questionButton.center.x = 271.5*SCALEW
            self.questionButton.center.y = 495.5*SCALEH
            self.questionButton.transform = CGAffineTransformMakeScale(1.3, 1.3)
            }, completion: nil)
        UIView.animateWithDuration(0.3, delay: 0.5, options: .CurveEaseOut, animations: {
            self.shareLabel.alpha = 1
            self.questionLabel.alpha = 1
        }, completion: nil)
    }
    
}

























